<template>
  <el-upload
    v-model:file-list="fileList"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
    list-type="picture-card"
    :limit="2"
    :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove"
    :on-exceed="handleOnexceed"
    :on-success="successFn"
  >
    <el-icon>
      <Plus />
    </el-icon>
  </el-upload>

  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
  <el-button type="primary" @click="clickFn">点击</el-button>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(true), console.log(1111)
  }, 1000)
})

const fileList = ref([
  {
    name: 'food.jpeg',
    url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
  }
])

const dialogImageUrl = ref('')
const dialogVisible = ref(false)

const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}
const handleOnexceed = () => {
  alert('多了')
}
const successFn = () => {
  console.log(fileList.value, 'success')
}
const clickFn = () => {
  setTimeout(() => {
    fileList.value.push({
      name: 'plant-2.png',
      url: '/images/plant-2.png'
    })
  }, 0)
}
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true
}
</script>
